(function() {
	var db = require("../../db/index");
	var config = require("../../config/config");
	var UserCon = iClass("User");
	var AdminCon = iClass("Admin");
	var ResponObj = require("./responObj");
	var notify = require("./notify");
	var role = {
		//获取已经登录的用户
		getUser: function(app) {
			app || (app = Object.getCci("app"));
			if (!app) {
				Throw("app对象无效，无法获取当前登录用户");
			}
			var loginerHash = app.cookies.get("user_loginSession");
			var loginInfo;
			if (loginerHash && (loginInfo = db.find_by_id(config.dbname.LoginHash, loginerHash))) {
				if ((loginInfo.last_login_time + config.autoSignUpTime) < +new Date) {
					db.remove(config.dbname.LoginHash, loginerHash);
					Throw("用户登录超时，请重新登录");
				} else if (loginInfo.type !== "user") {
					Throw("cookie值无效");
				} else {
					loginInfo.last_login_time = +new Date;
					db.update(config.dbname.LoginHash, loginInfo._id, loginInfo);
					//根据最后登录时间更新Cookie的有效期
					app.cookies.set("user_loginSession", loginerHash, {
						expires: new Date((+new Date) + config.autoSignUpTime)
					});
					return UserCon.get(loginInfo.user_id)
				}
			} else {
				Throw("用户未登录");
			}
		},
		//注册当前客户端为指定登录用户
		setUser: function(app, user) {
			//用户只能在一个地方登录，删除LoginHash中同用户名的信息
			db.remove_list(config.dbname.LoginHash, {
				user_id: user._id
			});
			//注入新的Session
			var hash = user._id.substr(2) + (+new Date).toString(16);
			app.cookies.set("user_loginSession", hash, {
				expires: new Date((+new Date) + config.autoSignUpTime)
			});
			var current_time = +new Date;
			db.insert(config.dbname.LoginHash, {
				_id: hash,
				user_id: user._id,
				type: "user",
				begin_login_time: current_time,
				last_login_time: current_time
			});
		},
		//删除用户的登录Session，退出登录
		removeUser: function(app, user) {
			//删除LoginHash中同用户名的信息
			db.remove_list(config.dbname.LoginHash, {
				user_id: user._id
			});
			//清空Cookie
			app.cookies.set("user_loginSession", "");
		},
		getUserSockSet: function(user_id) {
			var connsSet = notify.conns[user_id] || []
			return connsSet;
		},

		/*
		 * 管理员
		 */
		getAdmin: function(app) {
			var loginerHash = app.cookies.get("admin_loginSession");
			var loginInfo = db.find_by_id(config.dbname.LoginHash, loginerHash);
			if (loginInfo) {
				if ((loginInfo.last_login_time + config.autoSignUpTime) < +new Date) {
					db.remove(config.dbname.LoginHash, loginerHash);
					Throw("管理员登录超时，请重新登录");
				} else if (loginInfo.type !== "admin") {
					Throw("cookie值无效");
				} else {
					loginInfo.last_login_time = +new Date;
					db.update(config.dbname.LoginHash, loginerHash, loginInfo);
					//根据最后登录时间更新Cookie的有效期
					app.cookies.set("admin_loginSession", loginerHash, {
						expires: new Date((+new Date) + config.autoSignUpTime)
					});
					return AdminCon.get(loginInfo.admin_id)
				}
			} else {
				Throw("管理员未登录");
			}
		},
		//注册当前客户端为指定登录用户
		setAdmin: function(app, admin) {
			//用户只能在一个地方登录，删除LoginHash中同用户名的信息
			db.remove_list(config.dbname.LoginHash, {
				admin_id: admin._id
			});
			//注入新的Session
			var hash = admin._id.substr(2) + (+new Date).toString(16);
			app.cookies.set("admin_loginSession", hash, {
				expires: new Date((+new Date) + config.autoSignUpTime)
			});
			var current_time = +new Date;
			db.insert(config.dbname.LoginHash, {
				_id: hash,
				admin_id: admin._id,
				type: "admin",
				begin_login_time: current_time,
				last_login_time: current_time
			});
		},
		//删除用户的登录Session，退出登录
		removeAdmin: function(app, admin) {
			//删除LoginHash中同用户名的信息
			db.remove_list(config.dbname.LoginHash, {
				admin_id: admin._id
			});
			//清空Cookie
			app.cookies.set("admin_loginSession", "");
		},
		getAdminSockSet: function(admin_id) {
			var connsSet = notify.conns[admin_id] || []
			return connsSet;
		},
	};
	module.exports = role;
}());